package cn.com.zhbs.common.ucpaas;

import cn.com.zhbs.resource.MSGConfig;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import java.util.Properties;

public class SendMailUtil {

	public static void main(String[] args) throws Exception {
         String recAddress = "1410212983@qq.com";
         SendMailUtil.sendMailCode(recAddress,"有待更新的展示墙内容");

	}
	/**
	 * 发送验证码
	 * @return
	 * @throws Exception
	 */
	public static void sendMailCode(String recAddress,String content) throws Exception{
			
		// 发送的内容
		Properties properties = new Properties();
		
				properties.put("mail.transport.protocol", "smtp");// 连接协议
		
				properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
		
				properties.put("mail.smtp.port", 465);// 端口号
		
				properties.put("mail.smtp.auth", "true");
		
				properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
		
				properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
		
				// 得到回话对象
		
				Session session = Session.getInstance(properties);
		
				// 获取邮件对象
		
				MimeMessage message = new MimeMessage(session);
		
				// 设置发件人邮箱地址
				message.setFrom(new InternetAddress(MSGConfig.EMAIL_ADDRESS));
				// 设置收件人地址
				message.setRecipients(RecipientType.TO, new InternetAddress[] { new InternetAddress(recAddress) });
		
				// 设置邮件标题
				message.setSubject("后台管理系统","UTF-8");
			    message.setContent(content, "text/html;charset = utf-8");
				  
				// 得到邮差对象
				Transport transport = session.getTransport();
		
				// 连接自己的邮箱账户
				// IMP fzlhynlknsdkcabc
				transport.connect(MSGConfig.EMAIL_HOST,MSGConfig.EMAIL_ADDRESS, MSGConfig.EMAIL_PSD);// 密码为刚才得到的授权码  lybqxknqwgembjcj cbqyjyxtgxegbjhj
		
				// 发送邮件
				transport.sendMessage(message, message.getAllRecipients());

		
			}



}